package rmmk.datasources.parsing;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FilenameFilter;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import rmmk.algorithms.preprocessing.preprocessors.RemoveEmptyDocuments;
import rmmk.framework.datasources.Document;

public class DocumentReader2 {
	private RemoveEmptyDocuments red = new RemoveEmptyDocuments();
	private boolean addOrganizations = false;

	private Document parseSingleDocument(String file, String catalog, String base) {

		Document newDocument = new Document();

		newDocument.getTOPICS().add(catalog);
		URI newPath = null;

		try {
			newPath = this.getClass().getResource(base +"/"+catalog + "/" + file).toURI();
		} catch (URISyntaxException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		boolean body = false;
		boolean organization = false;

		try {
			BufferedReader reader = new BufferedReader(new FileReader(
					newPath.getPath()));
			String line = null;
			StringBuilder sbody = new StringBuilder();

			while ((line = reader.readLine()) != null) {
				if (!organization) {
					if (line.contains("Organization")) {
						if (this.addOrganizations) {
							String[] l2 = line.split(":");

							if (l2[1].length() > 0) {
								String[] cat = l2[1].split(",");

								for (String asd : cat)
									newDocument.getTOPICS().add(asd.trim());
							}
						}
					}

					organization = true;
				} else if (line.contains("Lines"))
					body = true;

				else if (body) {
					sbody.append(line);
				}
			}
			
			newDocument.setTEXT(sbody.toString());
			reader.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return newDocument;
	}

	public List<Document> readDefautDocuments() {
		String base = "/2";
		String[] dirctories = getCatalogList(base);

		ArrayList<Document> files = new ArrayList<Document>();

		for (String catalog : dirctories) {
			for (String file : getFileList(base+ "/" + catalog))
				files.add(this.parseSingleDocument(file, catalog, base));
		}
		
		Collections.shuffle(files);

		return red.process(files);
	}

	private String[] getCatalogList(String path) {

		URI newPath = null;

		try {
			newPath = this.getClass().getResource(path).toURI();
		} catch (URISyntaxException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		File file = new File(newPath.getPath());
		String[] directories = file.list(new FilenameFilter() {
			@Override
			public boolean accept(File current, String name) {
				return new File(current, name).isDirectory();
			}
		});

		return directories;
	}

	private String[] getFileList(String path) {

		URI newPath = null;

		try {
			newPath = this.getClass().getResource(path).toURI();
		} catch (URISyntaxException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		File file = new File(newPath.getPath());
		String[] files = file.list(new FilenameFilter() {
			@Override
			public boolean accept(File current, String name) {
				return !(new File(current, name).isDirectory());
			}
		});

		return files;
	}
}
